package com.rzcoding.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.rzcoding.dto.base.ResultBean;
import com.rzcoding.dto.base.ResultConst;
import com.rzcoding.dto.req.GoodsReqMo;
import com.rzcoding.entity.Goods;
import com.rzcoding.exception.BussinessException;
import com.rzcoding.service.GoodsService;
import com.rzcoding.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


@RestController
@RequestMapping("/api")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private OrderService orderService;

    @GetMapping(value = "/goods" )
    public ResponseEntity findPage(@RequestParam(name = "query", required = false) String goodsName,
                                   @RequestParam(name = "pagenum",required = false,defaultValue = "1") Integer pageNum,
                                   @RequestParam(name = "pagesize",required = false,defaultValue = "10") Integer pageSize){
        PageInfo<Goods> pageInfo = goodsService.selectByUsername(goodsName, pageNum, pageSize);
        JSONObject resp = new JSONObject().fluentPut("total", pageInfo.getTotal());
        resp.put("goods", pageInfo.getList());
        return new ResponseEntity<>(ResultBean.ok(ResultConst.GET_GOODS_LIST_SUCC, resp), HttpStatus.OK);
    }

    @DeleteMapping(value = "/goods/{id}" )
    public ResponseEntity delete(@PathVariable Integer id){
        goodsService.delete(id);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.DEL_GOODS_SUCC), HttpStatus.OK);
    }

    @PostMapping("/goods")
    public ResponseEntity add(GoodsReqMo goodsReqMo){
        goodsService.add(goodsReqMo);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.ADD_GOODS_SUCC), HttpStatus.OK);
    }

    /**
     * 实现文件上传
     * */
    @PostMapping("/upload")
    public ResponseEntity fileUpload(@RequestParam("file") MultipartFile file){
        if(file.isEmpty()){
            throw new BussinessException(HttpStatus.BAD_REQUEST.value(), ResultConst.FILE_NOT_EMPTY);
        }
        String fileName = file.getOriginalFilename();
        String path = orderService.getDirPath() + "/upload/";
        File dest = new File(path + fileName);
        if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
            JSONObject resp = new JSONObject().fluentPut("tmpPath", path + fileName);
            return new ResponseEntity<>(ResultBean.ok(ResultConst.UPLOAD_SUCC, resp), HttpStatus.OK);
        }catch (Exception e) {
            e.printStackTrace();
            throw new BussinessException(HttpStatus.BAD_REQUEST.value(), ResultConst.UPLOAD_FAIL);
        }
    }
}
